<?php
/**
 * /depot/library/models/Config/Xml.php
 * @author qqinxl
 * 2008/03/24
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class Config_Xml {
	/**
	 * 读取xml file
	 * @param string $_saveName 要保存成的名称
	 * @param string $_xmlFile 要读取的xml file
	 * @throws Exception
	 * @return Zend_Config_Xml
	 */
	static function readXml($_xmlFile, $_saveName = null) {
		if (! file_exists ( $_xmlFile ) or ! is_readable ( $_xmlFile )) {
			return null;
		}
		if (empty ( $_saveName )) {
			$_xmlObject = new Zend_Config_Xml ( $_xmlFile, null );
			return $_xmlObject;
		}
		if (Zend_Registry::isRegistered ( 'cache' )) {
			$_cache = Zend_Registry::get ( 'cache' );
		} else {
			$_cache = null;
		}
		if (! empty ( $_cache ) and ($_cache instanceof Zend_Cache or $_cache instanceof Zend_Cache_Core)) {
			$_xmlObject = $_cache->load ( $_saveName );
		} else {
			$_xmlObject = null;
		}
		if (! empty ( $_xmlObject ) and $_xmlObject instanceof Zend_Config_Xml) {
			return $_xmlObject;
		}
		if (Zend_Registry::isRegistered ( $_saveName )) {
			$_xmlObject = Zend_Registry::get ( $_saveName );
		} else {
			$_xmlObject = new Zend_Config_Xml ( $_xmlFile, null );
			Zend_Registry::set ( $_saveName, $_xmlObject );
		}
		if (! empty ( $_cache ) and ($_cache instanceof Zend_Cache or $_cache instanceof Zend_Cache_Core)) {
			$_cache->save ( $_xmlObject, $_saveName );
		}
		return $_xmlObject;
	}
	/**
	 * 读取ini file
	 * @param string $_saveName 要保存成的名称
	 * @param string $_iniFile 要读取的ini file
	 * @throws Exception
	 * @return Zend_Config_Ini
	 */
	static function readIni($_iniFile, $_saveName = null) {
		if (! file_exists ( $_iniFile ) or ! is_readable ( $_iniFile )) {
			return null;
		}
		if (empty ( $_saveName )) {
			$_iniObject = new Zend_Config_Ini ( $_iniFile, null );
			return $_iniObject;
		}
		if (Zend_Registry::isRegistered ( 'cache' )) {
			$_cache = Zend_Registry::get ( 'cache' );
		} else {
			$_cache = null;
		}
		if (! empty ( $_cache ) and ($_cache instanceof Zend_Cache or $_cache instanceof Zend_Cache_Core)) {
			$_iniObject = $_cache->load ( $_saveName );
		} else {
			$_iniObject = null;
		}
		if (! empty ( $_iniObject ) and $_iniObject instanceof Zend_Config_Ini) {
			return $_iniObject;
		}
		if (Zend_Registry::isRegistered ( $_saveName )) {
			$_iniObject = Zend_Registry::get ( $_saveName );
		} else {
			$_iniObject = new Zend_Config_Ini ( $_iniFile, null );
			Zend_Registry::set ( $_saveName, $_iniObject );
		}
		if (! empty ( $_cache ) and ($_cache instanceof Zend_Cache or $_cache instanceof Zend_Cache_Core)) {
			$_cache->save ( $_iniObject, $_saveName );
		}
		return $_iniObject;
	}
	/**
	 * @access public
	 * @param string $string
	 * @return boolean
	 */
	static function isTrue($string) {
		$string = trim ( $string );
		if (strtolower ( $string ) == "true" or strtolower ( $string ) == "y") {
			return true;
		}
		if (strtolower ( $string ) == "false" or strtolower ( $string ) == "n") {
			return false;
		}
		$string = ( int ) $string;
		return ! empty ( $string ) ? true : false;
	}
	static function isEmpty($key, $options = null) {
		if (! empty ( $options )) {
			if (isset ( $options [$key] )) {
				$value = trim ( $options [$key] );
				return empty ( $value ) ? true : false;
			}
			return true;
		} else {
			return empty ( $key ) ? true : false;
		}
	}

}